// pages/zhangjie/index.js
var app = getApp()
var CONFIG = require("../../utils/config.js")
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    has_more: true,
    count: 0,
    loadingTip: '没有更多数据',
    myinfo: [],
    booklist: [],
    onstatus: 1,
    zhangjie:false,
    cnum:1,
    zhang:1,
    info:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openId = wx.getStorageSync('openId')
    var info= app.globalData.bookinfo
    
    that.setData({
      zhangjie:true,
      info: app.globalData.bookinfo
   
    })
    wx.setNavigationBarTitle({
      title: app.globalData.booktitle
    });
    if (!openId) {
      app.login()
    } else {
     
    console.log("测试页面")
   var zhang=that.data.zhang
    var id =options.id

    that.queryUserBook(id)
    }
  },


  showDetail: function (e) {
    var that = this
    var bookid = e.currentTarget.dataset.id
    var booktitle = e.currentTarget.dataset.title
    var process = e.currentTarget.dataset.process
    var cnum = e.currentTarget.dataset.cnum
    var uid=wx.getStorageSync('openId')
    console.log(e)
    app.showDetail(bookid,cnum,process)

  },
  updateBuy:function(bookid,zhang){
    var that = this
    var url = CONFIG.URL.updateBuy
    var uid = wx.getStorageSync('openId')
    wx.request({
      url: url,
      data: {
        "wxuid": uid,
        "bookid": bookid,
        "bookzhang": zhang,
       
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        console.log("上传数据成功2")
      },
      faile: function (err) {
        console.log(err)
        console.log("上传数据shibai")
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  queryUserBook: function (id) {
    var that = this
    var uid=wx.getStorageSync('openId')
    var url = CONFIG.URL.queryUserBook
    var data = {
      "bookid": id,
      "wxuid":uid
    };
    CONFIG.post_from(url, data, function (res) {
      console.log("200+++")
      var info=that.data.info
      console.log(res.data.bookinfo)
      var newprocess = res.data.process
      console.log(res.data.process)
      if (res.statusCode == 200) {
       
        var nlength = info.num
        var datalist=[];
        for(var i=0;i<nlength;i++){
        datalist[i]={
          num:'',
          faceL:'',
          id:'',
          bewrite:'',
          auth:'',
          name:'',
          tag:'',
          process:0
        };
        datalist[i].num=i+1;
        datalist[i].name = info.name
        datalist[i].face = info.face
        datalist[i].id = info.id
        datalist[i].bewrite = info.bewrite
        datalist[i].auth = info.auth
        datalist[i].tag=info.tag
        datalist[i].zhang=true
     
        if (newprocess.length>0){
          for (var j = 0; j < newprocess.length;j++){
         
       if (datalist[i].num == newprocess[j].zhang){
          datalist[i].process = newprocess[j].zhang_process
          console.log(newprocess[j])
            }
          }
        }
        }
       that.setData({
          booklist:datalist
       })
      
      }
    }, function (err) {
      console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

 
})